package com.example.travelservlet.dynamic.controller;

import com.example.travelservlet.dynamic.service.DynamicService;
import com.example.travelservlet.entity.Dynamic;
import com.example.travelservlet.entity.DynamicDto;
import com.example.travelservlet.entity.DynamicPlus;
import com.example.travelservlet.user.domain.Result;
import com.example.travelservlet.user.domain.UserResult;
import com.example.travelservlet.utils.ResultUtil;
import com.example.travelservlet.utils.ResultUtilPlus1;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/dynamic")
public class DynamicController {

    @Autowired
    private DynamicService dynamicService;


    @PostMapping("/insertDynamic")
    public int insertDynamic(@Valid String dynamicJson) {
        //将json转换成dynamicdto对象
        Gson gson = new Gson();
        DynamicDto dynamicDto = gson.fromJson(dynamicJson, DynamicDto.class);
        System.out.println(dynamicDto.toString());
        int code = dynamicService.insertDynamic(dynamicDto);
        return code;
    }
    @PostMapping("/selectAllDynamic")
    public Result<List<DynamicDto>> selectAllDynamic(@Valid String userId){
        List<DynamicDto> dynamicDtos = dynamicService.selectAllDynamic(userId);
        return ResultUtil.success(dynamicDtos);
    }
    @PostMapping("/selectAllMyDynamic")
    public Result<List<DynamicDto>> selectAllMyDynamic(@Valid String userId){
        List<DynamicDto> dynamicDtos = dynamicService.selectAllMyDynamic(userId);
        return ResultUtil.success(dynamicDtos);
    }

    @PostMapping("/selectAllMyDynamicAndLike")
    public Result<List<DynamicDto>> selectAllMyDynamicAndLike(@Valid String userId, @Valid String mineId){
        List<DynamicDto> dynamicDtos = dynamicService.selectAllMyDynamicAndLike(userId, mineId);

        return ResultUtil.success(dynamicDtos);
    }

    @GetMapping("/findAllDynamic")
    private UserResult<List<Dynamic>> findAllDynamic(@RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                                                     @RequestParam(name = "size", required = false, defaultValue = "10") Integer size) {
        List<Dynamic> allDynamic = dynamicService.findAllDynamic();
        List<Dynamic> dynamicList = dynamicService.findAllDynamic1(page, size);
        return ResultUtilPlus1.success(dynamicList,allDynamic.size());
    }
    @GetMapping("/findAllDynamicPlus")
    private UserResult<List<DynamicPlus>> findAllDynamicPlus(@RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                                                     @RequestParam(name = "size", required = false, defaultValue = "10") Integer size) {
        List<DynamicPlus> allDynamic = dynamicService.findAllDynamic2();
        List<DynamicPlus> dynamicList = dynamicService.findAllDynamic3(page, size);
        return ResultUtilPlus1.success(dynamicList,allDynamic.size());
    }

    @GetMapping("/delete/{dynamicId}")
    public void deleteDynamicById(@PathVariable String dynamicId){
        dynamicService.deleteDynamicById(dynamicId);
    }

    @GetMapping("/findByKeyWord/{key}")
    private UserResult<List<DynamicPlus> >findDynamicByKeyWord(@RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                                                           @RequestParam(name = "size", required = false, defaultValue = "10") Integer size,@PathVariable String key){
        List<Dynamic> allDynamic = dynamicService.findDynamicByKeyWord(key);
        return ResultUtilPlus1.success(dynamicService.findDynamicByKeyWord1(key,page,size),allDynamic.size());
    }
    @PostMapping("/selectAllDynamicByLike")
    public Result<List<DynamicDto>> selectAllDynamicByLike(@Valid String userId){
        List<DynamicDto> dynamicDtos = dynamicService.selectAllDynamicByLike(userId);
        return ResultUtil.success(dynamicDtos);
    }
    @GetMapping("/deleteDynamicById/{dynamicId}")
    public int deleteDynamicByDynamicId(@PathVariable String dynamicId){
        return dynamicService.deleteDynamicByDynamicId(dynamicId);
    }
}
